package com.example.diandian.service.vod.controller.api;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.example.diandian.common.base.result.R;
import com.example.diandian.common.base.result.ResultCodeEnum;
import com.example.diandian.common.base.util.ExceptionUtils;
import com.example.diandian.service.base.exception.GuliException;
import com.example.diandian.service.vod.Utils.ConstantVodUtils;
import com.example.diandian.service.vod.Utils.InitVodCilent;
import com.example.diandian.service.vod.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(description="阿里云视频点播")
//@CrossOrigin //跨域
@RestController
@RequestMapping("/api/vod/media")
@Slf4j
public class ApiMediaController {

    @Autowired
    private VodService videoService;



    //根据视频id获取视频凭证
    @GetMapping("get-play-auth/{videoSourceId}")
    public R getPlayAuth(
            @ApiParam(value = "阿里云视频文件的id", required = true)
            @PathVariable String videoSourceId){
        try {
            //创建初始化对象
            System.out.println(videoSourceId);
            DefaultAcsClient client =
                    InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request设置视频id
            request.setVideoId(videoSourceId);
            //调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return R.ok().message("获取播放凭证成功").data("playAuth",playAuth);
        }catch(Exception e) {
            throw new GuliException(ResultCodeEnum.FETCH_PLAYAUTH_ERROR);
        }
    }
}
